/************************************************************************************
*项目：平衡小车模型项目
*时间：2019年8月30日
*作者：青竹
*版本：V1.0
*************************************************************************************/
#include "stm32f10x.h"
#include "delay.h"
#include "LED.h"
#include "power_key.h"
#include "key.h"
#include "systick.h"
#include "usart3.h"
#include "SoftTimer.h"

#include "motor.h"
#include "control.h"
#include "encoder.h"
#include "IOI2C.h"
#include "MPU6050.h"
#include "inv_mpu.h"
#include "inv_mpu_dmp_motion_driver.h"

#include "stdio.h"
#include "debug.h"

#define KEY_VAL_POWER_ON_OFF		1
#define SOFTTIME					10			//软件定时器时间基准

//===长短按键句柄
LongKey_Handle_Type stLongKey_Handle[LONG__KEY_MAX];

volatile uint8_t led_flash_time_cnt = 10;
volatile uint8_t time_1000ms_cnt = 0;

int pwm_data_A = 4000;
int pwm_data_B = 6000;

uint16_t encode_data_A = 0;
uint16_t encode_data_B = 0;

float Acceleration_Z;
float Angle_Balance, Gyro_Balance, Gyro_Turn;	

//短按按键处理
void PowerKey_Short_CB(void)
{
	static uint8_t i = 0;
	i++;
	if(i & 1)
	{
		led_flash_time_cnt = 10;
	}
	else
	{
		led_flash_time_cnt = 40;
	}

	pwm_data_A += 100;
	if(pwm_data_A > 6800)
	{
		pwm_data_A = 2000;
	}

	pwm_data_B += 200;
	if(pwm_data_B > 6800)
	{
		pwm_data_B = 4000;
	}
	#if DEBUG_PRINT_MOTOR
	printf("PowerKey_Short_CB\n");
	printf("pwm_data_A = %d\n", pwm_data_A);
	printf("pwm_data_B = %d\n", pwm_data_B);
	#endif
	Set_Pwm(pwm_data_A,pwm_data_B);
}

//长按按键处理
void PowerKey_LongStart_CB(void)
{
	//关机处理
	GPIO_ResetBits(GPIOA, GPIO_Pin_9);	
	#if DEBUG_PRINT
	printf("PowerKey_LongStart_CB\n");
	#endif
}

void Key_Analysis_50MS(void)
{
	KeyValueScan();
	
}

void SoftwareTimer_50MS(void)
{
	#if 1		//指示灯功能
	if(++time_1000ms_cnt >= led_flash_time_cnt)
	{
		time_1000ms_cnt = 0;
	}

	if(time_1000ms_cnt < (led_flash_time_cnt>>1))
	{
		LED1 = 1;
	}
	else
	{
		LED1 = 0;
	}
	#endif

	#if 0		// MPU6050
	Read_DMP();				   //===读取加速度、角速度、倾角
	Angle_Balance = Pitch;	   //===更新平衡倾角
	Gyro_Balance = gyro[1];	   //===更新平衡角速度
	Gyro_Turn = gyro[2];	   //===更新转向角速度
	Acceleration_Z = accel[2]; //===更新Z轴加速度计
	#endif

	encode_data_A = Read_Encoder(4);
	encode_data_B = Read_Encoder(2);
}


void SoftwareTimer_1000MS(void)
{
	#if DEBUG_PRINT
	printf("SoftwareTimer_1000MS\n");

	#endif

	#if DEBUG_PRINT_ENCODER
	printf("encode_data_A = %d\n",encode_data_A);
	printf("encode_data_B = %d\n",encode_data_B);

	#endif
	printf("pwm_data_A = %d\n", pwm_data_A);
	printf("pwm_data_B = %d\n", pwm_data_B);

	// printf("Angle_Balance = %f \n",Angle_Balance);
	// printf("Gyro_Balance = %f \n",Gyro_Balance);
	// printf("Gyro_Turn = %f \n",Gyro_Turn);
	// printf("Acceleration_Z = %f \n",Acceleration_Z);

	
}


int main(void)
{	
	Init_LEDpin();
	Power_pin();
	key_pin_init();

	

	MiniBalance_PWM_Init(7199, 0);	// PWM 电机驱动

	Encoder_Init_TIM2();			// 编码器驱动
	Encoder_Init_TIM4();
	SysTick_Init();					/* 配置SysTick 为10ms中断一次 */

	uart3_init(115200);				// 串口调试  串口调试中断优先级可以放低一点

	#if DEBUG_PRINT
	printf("power on! \n");
	#endif
	IIC_Init();
	#if DEBUG_PRINT
	printf("IIC init OK! \n");
	#endif
	MPU6050_initialize();			
	#if DEBUG_PRINT
	printf("MPU6050 init OK! \n");
	#endif
	DMP_Init();
	#if DEBUG_PRINT
	printf("DMP_Init OK! \n");
	#endif

	//长按按键初始化----按键句柄-------------按键键值------------------长按开始--长按保持---说明
	LongKeyEvents_Init(&stLongKey_Handle[0], KEY_VAL_POWER_ON_OFF, 3000 / 50, 0);	   //电源键
	
	//长按按键事件注册----按键句柄-------------按下回调---松手回调---长按开始回调----长按保持回调-------------------说明
	LongKeyEvents_Set_CB(&stLongKey_Handle[0], NULL, PowerKey_Short_CB, PowerKey_LongStart_CB, NULL);	

	SoftTimer_Init();										  // 初始化软件定时器
	SoftTimer_Create(50 / SOFTTIME, Key_Analysis_50MS);  			  // 50MS----按键解析
	SoftTimer_Create(50 / SOFTTIME, SoftwareTimer_50MS);			  // 50MS----软件定时器
	SoftTimer_Create(1000 / SOFTTIME, SoftwareTimer_1000MS);		  // 1000MS--软件定时器


	Set_Pwm(pwm_data_A,pwm_data_B);

	printf("pwm_data_A = %d\n", pwm_data_A);
	printf("pwm_data_B = %d\n", pwm_data_B);

	while(1)
	{

		SoftTimer_PerformCallback(); //执行软件定时器回调函数
	}
}






